using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Voodoo.Sauce.Internal.GDPR.UI
{
	[RequireComponent(typeof(Image))]
	public class SimpleCheckbox : MonoBehaviour, IPointerDownHandler, IEventSystemHandler
	{
		[SerializeField]
		private Sprite _uncheckedSprite;

		[SerializeField]
		private Color _uncheckedColor;

		[SerializeField]
		private Sprite _checkedSprite;

		[SerializeField]
		private bool _initialValue;

		[SerializeField]
		private string _identifier;

		public Toggle.ToggleEvent OnToggle;

		private bool _locked;

		private bool _checked;

		private Image _image;

		private Color _checkedColor;

		private void Awake()
		{
			_image = GetComponent<Image>();
			SetDefaultColor(Color.white);
		}

		private void OnEnable()
		{
			if (VoodooGDPRCanvas.IsInitialized)
			{
				if (_identifier == "ads")
				{
					SetChecked(VoodooGDPRCanvas.GetAdsConsent());
				}
				else if (_identifier == "analytics")
				{
					SetChecked(VoodooGDPRCanvas.GetAnalyticsConsent());
				}
				else
				{
					SetChecked(_initialValue);
				}
			}
		}

		public void Lock(bool value)
		{
			_locked = true;
			SetChecked(value: false);
			_image.color = new Color(1f, 1f, 1f, 0.5f);
		}

		public void OnPointerDown(PointerEventData eventData)
		{
			if (!_locked)
			{
				SetChecked(!_checked);
			}
		}

		private void SetChecked(bool value)
		{
			_checked = value;
			_image.sprite = ((!_checked) ? _uncheckedSprite : _checkedSprite);
			_image.color = ((!_checked) ? _uncheckedColor : _checkedColor);
			if (OnToggle != null)
			{
				OnToggle.Invoke(_checked);
			}
		}

		public bool IsChecked()
		{
			return _checked;
		}

		public void SetDefaultColor(Color color)
		{
			_checkedColor = color;
			_uncheckedColor = color;
			_image.color = Color.white;
		}
	}
}
